<template>
  <div class="sidebar-logo-container" :class="{'collapse':collapse}">
    <transition name="sidebarLogoFade">
      <router-link v-if="collapse" key="collapse" class="sidebar-logo-link" to="">
        <img
          v-if="currentLogo"
          :src="currentLogo"
          class="sidebar-logo"
          :style="{
            width: !accountVersion ? '87px' : 'auto',
            height: !accountVersion ? '16px' : 'auto'
          }"
        />
        <!-- <h1 v-else class="sidebar-title">{{ title }} </h1> -->
      </router-link>
      <router-link v-else key="expand" class="sidebar-logo-link" to="">
        <img
          v-if="currentLogo"
          :src="currentLogo"
          class="sidebar-logo"
          :style="{
            width: !accountVersion ? '87px' : 'auto',
            height: !accountVersion ? '16px' : 'auto'
          }"
        />
        <!-- <h1 class="sidebar-title">{{ title }} </h1> -->
      </router-link>
    </transition>
  </div>
</template>

<script>
import Vue from 'vue'
import { getAction } from '@/api/manage'
import { ACCOUNT_BOOK } from '@/store/mutation-types'

export default {
  name: 'SidebarLogo',
  props: {
    collapse: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      title: '云进销存',
      logo: require('@/assets/login/logo_xgh.png'),
      senior: require('@/assets/login/senior.png'), //高级版
      finance: require('@/assets/login/finance.png'), //标准财务版
      foundation: require('@/assets/login/foundation.png'), //必用版
      major: require('@/assets/login/major.png'), //专业版
      accountVersion: '',
      currentLogo: '',
    }
  },
  created() {
    this.getVersion()
  },
  methods: {
    async getVersion() {
      let accountBook = Vue.ls.get(ACCOUNT_BOOK)
      const bookId = accountBook && accountBook.id
      try {
        const res = await getAction('/accountbookinfo/xghAccountBook/getVersion', {
          id: bookId,
        })
        if (res.success) {
          this.accountVersion = res.result || '专业版'
          this.currentLogo = this.getCurrentLogo()
        }
      } catch (error) {
        console.error('获取版本信息失败:', error)
        this.currentLogo = this.logo
      }
    },
    getCurrentLogo () {
      switch (this.accountVersion) {
        case '必用版':
          return this.foundation
        case '专业版':
          return this.major
        case '高级版':
          return this.senior
        case '标准财务版':
          return this.finance
        default:
          return this.logo
      }
    }
  },
  mounted () {
    const that = this
    this.$EventBus.$on('changeAccountBook', function () {
      that.getVersion()
    })
  },
  beforeDestroy() {       
    this.$EventBus.$off('changeAccountBook')
  },
}
</script>

<style lang="scss" scoped>
.sidebarLogoFade-enter-active {
  transition: opacity 1.5s;
}

.sidebarLogoFade-enter,
.sidebarLogoFade-leave-to {
  opacity: 0;
}

.sidebar-logo-container {
  position: relative;
  width: 100%;
  height: 60px;
  line-height: 60px;
  // background: #2b2f3a;
  background: #006FFF;
  text-align: center;
  overflow: hidden;

  & .sidebar-logo-link {
    height: 100%;
    width: 100%;

    & .sidebar-logo {
      // width: 87px;
      // height: 16px;
      vertical-align: middle;
      margin-right: 12px;
    }

    & .sidebar-title {
      display: inline-block;
      margin: 0;
      color: #fff;
      font-weight: 600;
      line-height: 50px;
      font-size: 14px;
      font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
      vertical-align: middle;
    }
  }

  &.collapse {
    .sidebar-logo {
      margin-right: 0px;
    }
  }
}
</style>
